<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入菜品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="种类" prop="typeId">
        <el-select ref="typeId" v-model="queryParams.typeId" placeholder="请选择菜品种类" clearable>
          <el-option
            v-for="dict in menuTypeList"
            :key="dict.id"
            :label="dict.typeName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="会员价" prop="vipPrice">
        <el-input
          v-model="queryParams.vipPrice"
          placeholder="请输入菜品会员价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入菜品价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推荐指数" prop="recValue">
        <el-input
          v-model="queryParams.recValue"
          placeholder="请输入推荐指数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.shop_goods_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:restMenu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:restMenu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:restMenu:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="restEnable">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['shop:restMenu:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="restMenuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="缩略图" align="center" prop="imgAbbrev" width="100">
        <template v-slot="scope">
          <image-preview :src="scope.row.imgAbbrev" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="种类" align="center" prop="type">
        <template v-slot="scope">
          <span v-for="item in menuTypeList" v-if="item.id === scope.row.typeId">
            {{item.typeName}}
          </span>
<!--          <dict-tag :options="dict.type.shop_dish_type" :value="scope.row.type"/>-->
        </template>
      </el-table-column>
      <el-table-column label="会员价（元）" align="center" prop="vipPrice" >
        <template v-slot="scope">
          <span>{{(scope.row.vipPrice / 100).toFixed(2)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格（元）" align="center" prop="price" >
        <template v-slot="scope">
          <span>{{(scope.row.price / 100).toFixed(2)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="推荐指数" align="center" prop="recValue" />
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :options="dict.type.shop_goods_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:restMenu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:restMenu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listRestMenu, getRestMenu, delRestMenu, addRestMenu, updateRestMenu } from "@/api/shop/restMenu";
import {listMenuType} from "@/api/shop/menuType";

export default {
  name: "RestMenu",
  dicts: ['shop_dish_type', 'shop_goods_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 餐饮商户ID
      restId: null,
      // 餐饮菜品类型数据
      menuTypeList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 餐饮菜品表格数据
      restMenuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        restaurantId: null,
        name: null,
        type: null,
        vipPrice: null,
        price: null,
        recValue: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const restId = this.$route.query && this.$route.query.restId;
    if (restId) {
      this.restId = restId;
      this.queryParams.restaurantId = restId;
      this.getTypeList(restId);
    }
    this.getList();
  },
  activated() {
    this.getList();
  },
  computed: {
    // 是否禁用
    restEnable() {
      return this.restId;
    },
  },
  methods: {
    /** 查询餐饮菜品列表 */
    getList() {
      this.loading = true;
      listRestMenu(this.queryParams).then(response => {
        this.restMenuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询餐饮菜品类型 */
    getTypeList(restId) {
      listMenuType({restaurantId: restId}).then(response => {
        this.menuTypeList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        restaurantId: null,
        name: null,
        type: null,
        vipPrice: null,
        price: null,
        recValue: null,
        content: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加餐饮菜品";
      this.$tab.openPage("添加餐饮菜品", '/shop/restaurant/menu-add/index',{restId: this.restId});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const id = row.id || this.ids
      // getRestMenu(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改餐饮菜品";
      // });
      this.$tab.openPage("修改[" + row.name + "]菜品", '/shop/restaurant/menu-add/index',{restId: this.restId, menuId: row.id});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRestMenu(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRestMenu(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除餐饮菜品编号为"' + ids + '"的数据项？').then(function() {
        return delRestMenu(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/restMenu/export', {
        ...this.queryParams
      }, `restMenu_${new Date().getTime()}.xlsx`)
    },
    // 返回按钮
    handleClose() {
      const obj = { path: "/shop/restaurant/restaurant" };
      this.$tab.closeOpenPage(obj);
    },
  }
};
</script>
